package com.eastedu.superise.handler.permission;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eastedu.data.RequestParameter;
import com.eastedu.data.handler.AbstractPageHistoricalDataHandler;
import com.eastedu.repository.permissionassign.dto.ColumnPermissionIdChangeDTO;
import com.eastedu.repository.permissionassign.dto.PermissionRuleContentDTO;
import com.eastedu.repository.permissionassign.mapper.PermissionMapper;
import com.eastedu.repository.permissionassign.po.PermissionPO;
import com.eastedu.repository.permissionassign.vo.ColumnPermissionIdChangeVO;
import com.eastedu.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kuN
 */
@Slf4j
@Service
@DS("permission_assign")
public class PermissionAssignColumnIdUpdateHistoricalDataHandler extends AbstractPageHistoricalDataHandler<PermissionPO, RequestParameter> {
    private final PermissionMapper permissionMapper;

    public PermissionAssignColumnIdUpdateHistoricalDataHandler(PermissionMapper permissionMapper) {
        super(permissionMapper);
        this.permissionMapper = permissionMapper;
    }


    @Override
    protected void callback(PermissionPO permission, Map<String, Object> options) {
        ColumnPermissionIdChangeVO permissionIdChange = JsonUtil.parse(JsonUtil.toJsonString(options), ColumnPermissionIdChangeVO.class);

        permissionIdChange.getColumns().forEach(column -> {
            List<Long> newColumnId = column.getNewColumnId();
            String oldColumnId = String.valueOf(column.getColumnId());

            String newIdStr = newColumnId.stream().map(String::valueOf).collect(Collectors.joining(","));
            if (permission.getIdentity().contains(oldColumnId)) {
                permission.setIdentity(permission.getIdentity().replace(oldColumnId, newIdStr));
            }

            List<PermissionRuleContentDTO> contents = JsonUtil.parseList(permission.getRuleContent(), PermissionRuleContentDTO.class);
            PermissionRuleContentDTO ruleContent = contents.stream().filter(content -> content.getId().equals(oldColumnId)).findFirst().orElse(null);
            if(ruleContent == null){
                return;
            }
            contents.remove(ruleContent);

            newColumnId.forEach(id -> contents.add(PermissionRuleContentDTO.builder()
                            .id(String.valueOf(id))
                            .functions(ruleContent.getFunctions())
                    .build()));
            permission.setRuleContent(JsonUtil.toJsonString(contents));
        });
        permissionMapper.updateById(permission);
    }

    @Override
    protected Wrapper<PermissionPO> getWrapper(Serializable serializable, RequestParameter requestParameter) {
        ColumnPermissionIdChangeVO permissionIdChange = JsonUtil.parse(JsonUtil.toJsonString(requestParameter.getOptions()), ColumnPermissionIdChangeVO.class);

        LambdaQueryWrapper<PermissionPO> queryWrapper = Wrappers.lambdaQuery(PermissionPO.class)
                .eq(PermissionPO::getAppId, permissionIdChange.getAppId())
                .eq(PermissionPO::getCode, permissionIdChange.getCode())
                .eq(PermissionPO::getPermissionType, permissionIdChange.getPermissionType());
        List<ColumnPermissionIdChangeDTO> columns = permissionIdChange.getColumns();
        queryWrapper.and(wrapper -> {
            columns.forEach(column -> wrapper.or(w -> w.like(PermissionPO::getIdentity, column.getColumnId())));
        });
        return queryWrapper;
    }


}
